# -*- coding: utf-8 -*-  
import os
import re

#return a dictionary represent all the food topics and their contents
#foods = {
#          "食品名字" ： {
#             "tags" : ["tag1", "tag2", ...],
#            "intros" : {
#							"subIntroName1" : "subIntroString1",
#							"subIntroName2" : "subIntroString2", 
#							"subIntroName3" : "subIntroString3", 
#              "relatedLink" : ["link1", "link2", ...],
#             "link" : "urlForTheTopic", 
#            "number" : numberForTheTopic
#			"vector" : ["1", "2", "3", "4", "5", ...]
#          }
#
#			...
#}
def readContent():
	foods = {}
	
	#get content directory
	dirName = os.path.join( os.path.dirname( __file__ ), "static" )
	
	#get all exact content
	list = os.listdir( dirName )
	for item in list:
		food = {}
		contentPath = os.path.join( dirName, item )

		#get all tags for a topic
		tagsContentPath = os.path.join( contentPath, "tags.txt" )
		tagsFile = open( tagsContentPath, "r" )

		tagList = []
		for tag in tagsFile:
			tag = tag.strip("\n")
			tagList.append( tag )
		
		food["tags"] = tagList
		tagsFile.close()
		
		#get all introdutions for a topic if they exist
		introContentPath = os.path.join( contentPath, "introduction.txt" )
		if os.path.isfile( introContentPath ):
			introsFile = open( introContentPath, "r" )
			
			introDic = {}
			for introItem in introsFile:
				intro = introItem.split( "：" )
				introDic[intro[0].strip()] = intro[1].strip()
			
			food["intros"] = introDic
			introsFile.close()
		
		
		#get all the related link of the topic
		viewPath = os.path.join( contentPath, "view.txt" )
		viewFile = open( viewPath, "r" )
		pattern = re.compile( r"\d+" )
		relatedLinkList = []
		
		for relatedLine in viewFile:
			for relatedItem in pattern.findall( relatedLine ):
				relatedLinkList.append( relatedItem )
		
		food["relatedLink"] = relatedLinkList
		
		
		#get the link and name of the topic
		linkPath = os.path.join( contentPath, "link.txt" )
		linkFile = open( linkPath, "r" )
		
		name = linkFile.readline().strip("\n")
		link = linkFile.readline().strip("\n")
		food["link"] = link
		linkFile.close()
		
		#get the number of the topic in baidu baike
		food["number"] = item
		
		#get the vector of the topic
		linkPath = os.path.join( contentPath, "tf-idf.txt" )
		linkFile = open( linkPath, "r" )	
		
		vector = []
		for line in linkFile:
			vector.append( float(line.strip()) )
		
		food["vector"] = vector
		
		#store the topic as an element of the 'foods' dictionary
		foods[name] = food
		
	return foods

	
#return a dictionary represent the result of cutting all the names
#cutNameResult = {
#					"被分词后的一个词组名字" :
#					[ "contentNum1", "contentNum2", ... ]
#}
def readCutNameResult():
	cutNameResult = {}
	filePath = os.path.join( os.path.dirname( __file__ ), "cutNameResultSorted.txt" )	
	file = open( filePath, "r" )
	
	for line in file:
		list = []
		splitList = line.strip("\n").split(" ")
		
		i = 0;
		while i < int(splitList[1]):
			list.append( splitList[i+2] )
			i += 1
		
		cutNameResult[splitList[0]] = list
		
	return cutNameResult
	file.close()
	

if __name__ == "__main__":
	cutNameResult = readCutNameResult()
	
	"""
	for name in cutNameResult:
		print name.decode("utf-8") + " " + str(cutNameResult[name]) + "\n"
	"""
	
	
	